package com.ruoyi.project.system.course.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.courseClass.domain.CourseClass;
import com.ruoyi.project.system.courseClass.service.ICourseClassService;
import com.ruoyi.project.system.grades.domain.ClassGrades;
import com.ruoyi.project.system.student.domain.ClassStudent;
import com.ruoyi.project.system.student.service.IClassStudentService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.course.mapper.CourseMapper;
import com.ruoyi.project.system.course.domain.Course;
import com.ruoyi.project.system.course.service.ICourseService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 课程Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-07
 */
@Service
public class CourseServiceImpl implements ICourseService
{
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private IUserService userService;

    @Autowired
    private ICourseClassService courseClassService;

    @Autowired
    private IClassStudentService classStudentService;

    /**
     * 查询课程
     *
     * @param id 课程ID
     * @return 课程
     */
    @Override
    public Course selectCourseById(Long id)
    {
        return courseMapper.selectCourseById(id);
    }

    /**
     * 查询课程列表
     *
     * @param course 课程
     * @return 课程
     */
    @Override
    public List<Course> selectCourseList(Course course)
    {
        return courseMapper.selectCourseList(course);
    }

    /**
     * 新增课程
     *
     * @param course 课程
     * @return 结果
     */
    @Override
    public int insertCourse(Course course)
    {
        User user = userService.selectUserById(course.getTeacherId());
        course.setTeacherName(user.getUserName());
        course.setCreateTime(DateUtils.getNowDate());
        return courseMapper.insertCourse(course);
    }

    /**
     * 修改课程
     *
     * @param course 课程
     * @return 结果
     */
    @Override
    public int updateCourse(Course course)
    {
        User user = userService.selectUserById(course.getTeacherId());
        course.setTeacherName(user.getUserName());
        course.setUpdateTime(DateUtils.getNowDate());
        return courseMapper.updateCourse(course);
    }

    /**
     * 删除课程对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCourseByIds(String ids)
    {
        return courseMapper.deleteCourseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程信息
     *
     * @param id 课程ID
     * @return 结果
     */
    @Override
    public int deleteCourseById(Long id)
    {
        return courseMapper.deleteCourseById(id);
    }

    @Override
    public int manageGrade(Course course) {
        List<String> classIds = Arrays.asList(course.getClassIds().split(","));
        courseClassService.deleteByCourseId(course.getId());
        if(classIds.size() > 0){
            for (String classId : classIds) {
                CourseClass courseClass = new CourseClass();
                courseClass.setClassId(Long.valueOf(classId));
                courseClass.setCourseId(course.getId());
                courseClassService.insertCourseClass(courseClass);
            }
        }
        return 1;
    }

    @Override
    public int manageStudent(ClassGrades classGrades) {
        List<String> studentIds = Arrays.asList(classGrades.getStudentIds().split(","));
        classStudentService.deleteByClassId(classGrades.getId());
        if(studentIds.size() > 0){
            for (String studentId : studentIds) {
                ClassStudent classStudent = new ClassStudent();
                classStudent.setStudentId(Long.valueOf(studentId));
                classStudent.setClassId(classGrades.getId());
                classStudentService.insertClassStudent(classStudent);
            }
        }
        return 1;
    }
}
